package com.example.autopia.view.Adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.autopia.databinding.ViewholderEmptyItemBinding
import com.example.autopia.databinding.ViewholderNewsActionBinding
import com.example.autopia.databinding.ViewholderNewsHotnewsBinding
import com.example.autopia.databinding.ViewholderNewsPostOutPhotoBinding
import com.example.autopia.databinding.ViewholderNewsPostWithPhotoBinding
import com.example.autopia.model.Item.News
import com.example.autopia.view.Listeners.ActionNewsListener
import com.example.autopia.view.Listeners.PostNewsListener
import com.example.autopia.view.ViewHolders.News.ActionNewsViewHolder
import com.example.autopia.view.ViewHolders.News.EmptyViewHolder
import com.example.autopia.view.ViewHolders.News.HotNewsViewHolder
import com.example.autopia.view.ViewHolders.News.PhotoNewsViewHolder
import com.example.autopia.view.ViewHolders.News.TextNewsViewHolder

class NewsAdapter :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var oldList = emptyList<News>().toMutableList()
    private lateinit var listenerAction: ActionNewsListener
    private lateinit var listenerPost: PostNewsListener
    private  var likeNews = emptyList<String>().toMutableList()

    fun attachListenerAction(listener: ActionNewsListener) {
        this.listenerAction = listener
    }
    fun attachListenerPost(listener: PostNewsListener) {
        this.listenerPost = listener
    }
    fun deletePost(item: News) {
        val pos = oldList.indexOf(item)
        oldList.removeAt(pos)
        notifyItemRemoved(pos)
    }

    companion object {
        const val ACTION_VIEW = 0
        const val HOTNEWS_VIEW = 3
        const val TEXT_VIEW = 1
        const val IMAGE_VIEW = 2
        const val EMPTY_VIEW = 4
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            ACTION_VIEW -> ActionNewsViewHolder(
                ViewholderNewsActionBinding.inflate(
                    LayoutInflater.from(
                        parent.context
                    ), parent, false
                )
            )

            HOTNEWS_VIEW -> HotNewsViewHolder(
                ViewholderNewsHotnewsBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),inflater
            )

            TEXT_VIEW -> TextNewsViewHolder(
                ViewholderNewsPostOutPhotoBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),inflater,likeNews
            )
            IMAGE_VIEW -> PhotoNewsViewHolder(
                ViewholderNewsPostWithPhotoBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                ),inflater,likeNews
            )

            EMPTY_VIEW -> EmptyViewHolder(
                ViewholderEmptyItemBinding.inflate(
                    LayoutInflater.from(
                        parent.context
                    ), parent, false
                )
            )
            else -> throw IllegalArgumentException("invalid item type")
        }
    }

    override fun getItemCount(): Int {
        return oldList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        return when (oldList[position].viewType) {
            ACTION_VIEW -> (holder as ActionNewsViewHolder).bind(
                oldList[position],listenerAction
            )
            HOTNEWS_VIEW -> (holder as HotNewsViewHolder).bind(
                oldList[position],
            )
            TEXT_VIEW -> (holder as TextNewsViewHolder).bind(
                oldList[position],listenerPost
            )
            IMAGE_VIEW -> (holder as PhotoNewsViewHolder).bind(
                oldList[position],listenerPost
            )
            EMPTY_VIEW -> (holder as EmptyViewHolder).bind(

            )
            else -> throw IllegalArgumentException("invalid item type")
        }
    }

    override fun getItemViewType(position: Int): Int {
        return oldList[position].viewType
    }

    fun setData(newList: List<News>, likeNews: List<String>) {
        oldList = newList.toMutableList()
        this.likeNews = likeNews.toMutableList()
        notifyDataSetChanged()
    }
}